#include "afsapsparser.h"
#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QtXml>
AfsapsParser::AfsapsParser(QObject *parent) :
    QObject(parent)
{

    mNetManager = new QNetworkAccessManager;


}

void AfsapsParser::setSource(const QString &url)
{

    qDebug()<<url;
    QFileInfo info(url);
    QNetworkReply * reply =  mNetManager->get(QNetworkRequest(url));
    reply->setObjectName(info.fileName());

    connect(reply,SIGNAL(finished()),this,SLOT(parser()));



}

void AfsapsParser::parser(const QString &filename)
{
    QNetworkReply * reply = qobject_cast<QNetworkReply*> (sender());


    qDebug()<<reply->objectName();
    QFile file(reply->objectName());
     if (!file.open(QIODevice::ReadWrite| QIODevice::Text))
         return;

     QTextStream out(&file);
     out <<reply->readAll();

     file.close();


     // NOW START THE PARSER

     QDomDocument doc("mydocument");

      doc.setContent(&file);



      // print out the element names of all elements that are direct children
      // of the outermost element.
      QDomElement docElem = doc.documentElement();






     QDomNode n = docElem.childNodes().at(1);


      while(!n.isNull()) {
          QDomElement e = n.toElement(); // try tco convert the node to an element.
          if(!e.isNull()) {
              qDebug() << qPrintable(e.tagName());
          }
          n = n.nextSibling();
      }





    qDebug()<<"saved into ";


}
